package com.qst.web;

import com.qst.po.JsonData;
import com.qst.po.Lesson;
import com.qst.po.PageData;
import com.qst.service.ILessonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class LessonController {
    @Autowired
    private ILessonService lessonService;
    @RequestMapping("tolessonlist")
    public String list(){
        return "lessonlist";
    }
    @RequestMapping("lessonlists")
    @ResponseBody
    public PageData<Lesson> lessonlists(int page, int limit){
        PageData<Lesson> data=new PageData<Lesson>();
        data.setCode(0);
        data.setMsg("");
        data.setCount(lessonService.getTotalCount());
        data.setData(lessonService.getlessonsbypage(page,limit));
        return data;
    }
    @RequestMapping("toaddlesson")
    public String toaddlesson(){
        return "addlesson";
    }
    @RequestMapping("addlesson")
    @ResponseBody
    public JsonData addlesson(Lesson lesson){
        JsonData data=new JsonData();
        int count=lessonService.addlesson(lesson);
        if(count>0){
            data.setCode(200);
        }else{
            data.setCode(500);
            data.setMsg("添加失败");
        }
        return data;
    }


    @RequestMapping("getalllessons")
    @ResponseBody
    public List<Lesson> getalllessons(){
        List<Lesson> lessons =lessonService.getalllessons();
        return lessons;
    }
    @RequestMapping("toupdatelesson")
    public String toupdatelesson(int id, HttpServletRequest request){
        Lesson lesson=lessonService.getlessonsbyid(id);
        request.setAttribute("updateles",lesson);
        return "updatelesson";
    }
    @RequestMapping("updatelesson")
    @ResponseBody
    public JsonData updatelesson(Lesson lesson){
        JsonData data=new JsonData();
        int count=lessonService.updatelesson(lesson);
        if(count>0){
            data.setCode(200);
        }else{
            data.setCode(500);
            data.setMsg("修改失败");
        }
        return data;
    }
    @RequestMapping("deletelesson")
    @ResponseBody
    public JsonData deletelesson(int id){
        JsonData data =new JsonData();
        int count =lessonService.deletelesson(id);
        if(count>0){
            data.setCode(200);
        }else{
            data.setCode(500);
            data.setMsg("删除失败，请重新删除");
        }
        return data;
    }

}
